package com.inspsys.inspection.service.impl;

import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.inspsys.inspection.mapper.InspectionPlanMapper;
import com.inspsys.inspection.mapper.InspectionTaskMapper;
import com.inspsys.common.utils.DateUtils;
import com.inspsys.inspection.domain.InspectionPlan;
import com.inspsys.inspection.domain.InspectionTask;
import com.inspsys.inspection.service.IInspectionPlanService;

/**
 * 巡检计划Service业务层处理
 *
 * @author inspsys
 * @date 2025-01-08
 */
@Service
public class InspectionPlanServiceImpl implements IInspectionPlanService {

    @Autowired
    private InspectionPlanMapper inspectionPlanMapper;

    @Resource
    private InspectionTaskMapper inspectionTaskMapper;

    /**
     * 查询巡检计划
     *
     * @param id 巡检计划主键
     * @return 巡检计划
     */
    @Override
    public InspectionPlan selectInspectionPlanById(Long id) {
        return inspectionPlanMapper.selectInspectionPlanById(id);
    }

    /**
     * 查询巡检计划列表
     *
     * @param inspectionPlan 巡检计划
     * @return 巡检计划
     */
    @Override
    public List<InspectionPlan> selectInspectionPlanList(InspectionPlan inspectionPlan) {
        return inspectionPlanMapper.selectInspectionPlanList(inspectionPlan);
    }

    /**
     * 新增巡检计划
     *
     * @param inspectionPlan 巡检计划
     * @return 结果
     */
    @Override
    public int insertInspectionPlan(InspectionPlan inspectionPlan) {
        StringBuffer orderNumerSb = new StringBuffer(DateUtils.dateTimeNow("yyyyMMddHHmm"));
        orderNumerSb.append("/").append(inspectionPlan.getTaskType());
        orderNumerSb.append("/").append(inspectionPlan.getPlatform());
        orderNumerSb.append("/").append(inspectionPlan.getSection());
        inspectionPlan.setOrderNumber(orderNumerSb.toString());
        return inspectionPlanMapper.insertInspectionPlan(inspectionPlan);
    }

    /**
     * 修改巡检计划
     *
     * @param inspectionPlan 巡检计划
     * @return 结果
     */
    @Override
    public int updateInspectionPlan(InspectionPlan inspectionPlan) {
        return inspectionPlanMapper.updateInspectionPlan(inspectionPlan);
    }

    /**
     * 批量删除巡检计划
     *
     * @param ids 需要删除的巡检计划主键
     * @return 结果
     */
    @Override
    public int deleteInspectionPlanByIds(Long[] ids) {
        return inspectionPlanMapper.deleteInspectionPlanByIds(ids);
    }

    /**
     * 删除巡检计划信息
     *
     * @param id 巡检计划主键
     * @return 结果
     */
    @Override
    public int deleteInspectionPlanById(Long id) {
        return inspectionPlanMapper.deleteInspectionPlanById(id);
    }

    /**
     * 发布巡检计划信息
     *
     * @param id 巡检计划主键
     * @return 结果
     */
    @Override
    public int releaseInspectionPlanById(Long id) {
        InspectionPlan plan = inspectionPlanMapper.selectInspectionPlanById(id);
        InspectionTask task = new InspectionTask();
        task.setOrderNumber(plan.getOrderNumber());
        List<InspectionTask> list = inspectionTaskMapper.selectInspectionTaskList(task);
        // 如果已经生成巡检任务，不允许再次发布
        if (!CollectionUtils.isEmpty(list)) {
            return 2;
        }
        task.setTaskType(plan.getTaskType());
        task.setPlatform(plan.getPlatform());
        task.setSection(plan.getSection());
        task.setSpecifications(plan.getSpecifications());
        task.setTaskStartTime(plan.getPlanStartTime());
        task.setTaskEndTime(plan.getPlanEndTime());

        // 生成 200 以内的随机数（暂时）
        Random random = new Random();
        task.setInspect(random.nextInt(200) + "");
        task.setGather(random.nextInt(200) + "");
        task.setMaintenance(random.nextInt(200) + "");
        task.setDealWith(random.nextInt(200) + "");
        task.setReport(random.nextInt(200) + "");
        task.setReview(random.nextInt(200) + "");
        return inspectionTaskMapper.insertInspectionTask(task);
    }
}
